import webSocket from '@ohos.net.webSocket';
import router from '@system.router';
let ws = webSocket.createWebSocket();
let url;
ws.on('message', (err, value) => {
    let result = JSON.parse(value);

    console.log("on message, message----:" + value);
    if(err) {
        console.log("message fail, err:" + JSON.stringify(err))
    }
});
ws.on('error', (err) => {

    console.info("on error, error:" + JSON.stringify(err));

});
export default {
    data: {
        currentPageIndex: 0, /*当前页面位置*/
        tab0selected: true, /**/
        tab1selected: false,
        tab2selected: false,
        tab3selected: false,
        tab4selected: false,
        isdisable: true,
        lampTextSN:'',
        result: '',
        role: '',
        token: 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJyb2xlIjoiMCIsImV4cCI6MTY0NTI0OTQ3MywidXNlcklkIjoiMTQ4MjY1MDY3ODk3NDk0MzIzMyIsInVzZXJuYW1lIjoiUVHlhYnmo43oioLlv6vkuZAifQ.j-kqvMGa2CuRBVjE72BYp81Pxf_--JUqrgFjbsZMWiM'
    },

    onInit(){
        console.info("data----"+ this.result.username + 'role----' + this.role);
        this.token = this.result.token;
        url = "ws://dreamwake.top:8888/chat/" + this.result.userId+'?token='+this.token;
        ws.connect(url, (err, value) => {

            if (!err) {
                console.log("connect success pig------"+value);
            } else {
                console.log("connect fail, err:" + JSON.stringify(err))
            }
        });
        ws.on('open', (err, value) => {
            console.log("on open, status-----:" + value.status + ", message:" + JSON.stringify(value));
        });

    },


    onTabSelected(index) {
        if (Number(index) != this.currentPageIndex) {
            this.currentPageIndex = Number(index);
            console.info("onTabSelected:" + index);
        } else {
            console.info("onTabReSelected:" + index);
            return;
        }

        this.change2Tab(); //切换按钮
        this.change2Page(); //切换页面
    },


    lampText(lampSN){

        this.lampTextSN = lampSN;
        console.info("lampTextSN---"+ this.lampTextSN)
    },

    changeDisable(){
        this.isdisable = !this.isdisable;
        console.info("changeDisable--" + this.isdisable)
    },

    onPageChange(e) {
        this.currentPageIndex = Number(e.index);

        this.change2Tab(); //切换按钮
    },
    change2Page() {
        console.info("change2Page:" + this.currentPageIndex);

        this.$element('pageSwiper').swipeTo({
            index: this.currentPageIndex,
        }); //切换页面
    },
    change2Tab() {
        console.info("change2Tab:" + this.currentPageIndex);
        for (var i = 0; i < 5; i++) {
            var param = "tab" + i + "selected";
            if (this.currentPageIndex == i) {
                //选中位置
                this[param] = true;
            } else {
                //非选中位置
                this[param] = false;
            }
        }
    }
}